// components/search/search.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 模块名称
    table: {
      type: String,
      value: '',
    },
    // 是否显示高级查询图标
    adv: {
      type: Boolean,
      value: false,
    },
    // 搜索关键词数组
    keyList: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        if(newVal.length) {
          let arr = []
          newVal.forEach(item => item.field_name && arr.push(item.field_name))
          this.setData({
            plc: `搜索${arr.join('、')}`,
          })
        }
      },
    },
    // 高级查询tab
    advList: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        if(!newVal.length) return
        let { advSearchList, } = this.data
        advSearchList[0].optionList = [
          { combination_query_json: 'all', combination_name: '全部', },
          ...newVal,
        ]
        this.setData({
          advSearchList,
        })
      },
    },
    // 快速查询筛选条件
    searchList: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        if(newVal.length) {
          let dfltValArr = newVal.filter(item => item.start || item.end || item.dfltdate)
          if(dfltValArr.length) {
            this.setData({
              quickVal: dfltValArr,
            })
          }
        }
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    plc: '请输入关键词进行搜索',
    searchVal: '',

    advFlag: false,           // 高级查询弹层弹出标识
    quickFlag: false,         // 快速查询弹层弹出标识
    quickVal: [],             // 快速查询值
    advVal: '',               // 高级查询值

    advSearchList: [
      { field_name: '高级查询', type: 'picklist', field_id: 'adv', optionList: [
        { combination_query_json: 'all', combination_name: '全部', },
      ], optionId: 'combination_query_json', optionName: 'combination_name', },
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 双向绑定 报警告处理
    inputIpt(e) {},
    blurIpt(e) {
      let { searchVal, } = this.data,
          val = e.detail.value
      if(searchVal !== val) {
        this.setData({
          searchVal: val,
        })
      }
    },

    // 点击了键盘的搜索
    search(e) {
      // if(!this.data.searchVal) return
      let { table, keyList, searchVal, } = this.data,
          searchWhere = []
      searchVal = e.detail.value
      this.data.searchVal = searchVal
      if(table) table += '.'
      if(searchVal) {
        keyList.forEach(item => {
          let { field_id, } = item
          searchWhere.push({
            CompareValue: searchVal,
            CompareWith: `${table}${field_id}`,
            Operator: 'like',
            or: 1,
          })
        })
      }

      this.triggerEvent('search', searchWhere)
      this.triggerCombSearch()
    },
    // 清空输入框
    emptyIpt(e) {
      let { searchVal, } = this.data
      this.setData({
        searchVal: '',
      })
      if(!searchVal) return
      this.triggerEvent('search', [])
      this.triggerCombSearch()
    },
    // 点击了 高级查询，显示高级查询弹层
    showAdv(e) {
      if(!this.data.adv) return
      this.setData({
        advFlag: !this.data.advFlag,
      })
    },
    // 点击了 快速查询，显示快速查询弹层
    showQuick(e) {
      this.setData({
        quickFlag: !this.data.quickFlag,
      })
    },

    // 快速查询保存
    searchQuick(e) {
      this.setData({
        quickVal: e.detail,
      })
      this.triggerEvent('quicksearch', e.detail)
      this.triggerCombSearch()
    },
    // 高级查询保存
    searchAdv(e) {
      let adv = e.detail.adv,
          val = !adv ? '' : adv.val === 'all' ? '' : adv.val,
          obj = {
            advVal: val,
          }
      if(!val) {
        obj.searchVal = ''
        obj.quickVal = []
        this.selectComponent('#quick').reset()
      }
      this.setData(obj)
      this.triggerEvent('advsearch', val)
      this.triggerCombSearch()
    },
    triggerCombSearch() {
      let { searchVal, quickVal, advVal, keyList, table, } = this.data,
          searchObj = {
            iptVal: searchVal,
            search: '',
            quick: quickVal,
            adv: advVal,
            comb: [],
          }
          
      let searchWhere = []
      if(table) table += '.'
      if(searchVal) {
        keyList.forEach(item => {
          let { field_id, } = item
          searchWhere.push({
            CompareValue: searchVal,
            CompareWith: `${table}${field_id}`,
            Operator: 'like',
            or: 1,
          })
        })
      }
      searchObj.search = searchWhere

      let advArr = []
      if(advVal) {
        advVal = JSON.parse(advVal)
        advArr = this.loopAdv(advVal)
      }
      searchObj.comb = [ ...searchWhere, ...quickVal, ...advArr, ]
      
      this.triggerEvent('combsearch', searchObj)
    },
    loopAdv(advVal) {
      let rtnArr = [ ...advVal.filters, ]
      if(advVal.children && advVal.children.length) {
        advVal.children.forEach(child => {
          rtnArr.push(this.loopAdv(child))
        })
      }
      return rtnArr.flat(Infinity)
    },
  },
})
